<template>
  <a-card :bordered="false">
    <a-spin :spinning="isLoading">
      <div class="card-title">{{ $route.meta.title }}</div>
      <div class="ant-table ant-table-scroll-position-left ant-table-default ant-table-bordered">
        <div class="ant-table-content">
          <div class="ant-table-body">
            <table>
              <thead class="ant-table-thead">
                <tr>
                  <template v-for="(item, index) in columns">
                    <th :style="`text-align:${item.align};width:${item.width}`">
                      <div class="ant-table-header-column">
                        <span class="ant-table-column-title">{{ item.title }}</span>
                      </div>
                    </th>
                  </template>
                </tr>
              </thead>
              <tbody class="ant-table-tbody">
                <template v-for="(item, index) in mergeOrderList">
                  <tr class="order-empty">
                    <td colspan="6"></td>
                  </tr>
                  <tr>
                    <td colspan="6">
                      <div class="tabOrdeno">
                        <div class="tabOrdeno_l">
                          <span class="mr-20">合并订单号：{{ item.c_order_no }}</span>
                        </div>
                        <div class="tabOrdeno_r">
                          <a-button @click="handleDelete(item)">取消合并</a-button>
                        </div>
                      </div>
                    </td>
                  </tr>
                  <template v-for="(orderItm, orderIdx) in item.orderList">
                    <tr v-for="(goodsItm, goodsIdx) in orderItm.goods">
                      <td>
                        {{ orderItm.order_no }}
                      </td>
                      <td>
                        <GoodsItem :data="{
    image: goodsItm.goods_image,
    imageAlt: '商品图片',
    title: goodsItm.goods_name,
    goodsProps: goodsItm.goods_props,
    remark: orderItm.merchant_remark
  }" />
                      </td>
                      <td>
                        {{ orderItm.name }}
                      </td>
                      <td>
                        {{ orderItm.phone }}
                      </td>
                      <td>
                        {{ orderItm.detail }}
                      </td>
                      <td>
                        <div class="actions">
                          <router-link v-if="$auth('/order/detail')"
                            :to="{ path: '/order/detail', query: { orderId: orderItm.order_id } }">详情</router-link>
                        </div>
                      </td>
                    </tr>
                  </template>
                </template>
              </tbody>
            </table>
          </div>
          <!-- 空状态 -->
          <a-empty v-if="!mergeOrderList.length" :image="simpleImage" />
        </div>
      </div>
      <!-- 分页器 -->
      <div v-if="mergeOrderList.length" class="pagination">
        <a-pagination :current="page" :pageSize="per_page" :total="total" @change="onChangePage" />
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import { Empty } from 'ant-design-vue'
import { assignment } from '@/utils/util'
import * as Api from '@/api/order'
import { GoodsItem } from '@/components/Table'

// 表格字段
const columns = [
  {
    title: '订单号',
    align: 'left',
    dataIndex: 'order_no',
    width: '200px',
    scopedSlots: { customRender: 'order_no' }
  },
  {
    title: '商品信息',
    align: 'center',
    dataIndex: 'goods',
    width: '200px',
    scopedSlots: { customRender: 'goods' }
  },
  {
    title: '买家',
    align: 'center',
    dataIndex: 'user',
    width: '120px',
    scopedSlots: { customRender: 'user' }
  },
  {
    title: '手机号',
    align: 'center',
    dataIndex: 'tel',
    width: '180px',
    scopedSlots: { customRender: 'tel' }
  },
  {
    title: '地址',
    align: 'center',
    dataIndex: 'address',
    width: '280px',
    scopedSlots: { customRender: 'address' }
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    width: '180px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'Index',
  components: {
    GoodsItem
  },

  data() {
    return {
      // 正在加载
      isLoading: false,
      // 表格字段
      columns,
      // 当前页码
      page: 1,
      // 列表数据
      mergeOrderList: [],
      orderlistLength: 0,
      total: 0,
      per_page: 10

    }
  },
  beforeCreate() {
    // 批量给当前实例赋值
    assignment(this, {
      simpleImage: Empty.PRESENTED_IMAGE_SIMPLE
    })
  },
  created() {
    // 初始化页面
    this.init()
  },
  watch: {
    // 监听路由变化
    $route() {
      this.init()
    }
  },
  methods: {

    // 初始化页面
    init() {
      this.handleRefresh(true)
    },


    // 获取列表数据
    getList() {
      this.isLoading = true
      this.orderlistLength = 0
      return Api.getCombOrder({ page: this.page })
        .then(response => {
          this.mergeOrderList = response.data.data
          this.total = response.data.total
          this.per_page = response.data.per_page
        })
        .finally(() => {
          this.isLoading = false
        })
    },

    // 移除合并订单
    handleDelete(item) {
      const that = this
      const modal = this.$confirm({
        title: `将所有订单从合并订单(${item.c_order_no})里移除？`,
        content: '',
        onOk() {
          return Api.updateCombination({ c_order_no: item.c_order_no })
            .then((result) => {
              that.handleRefresh()
              that.$message.success('移除成功', 1.5)
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      bool && (this.page = 1)
      this.getList()
    },

    // 翻页事件
    onChangePage(current) {
      this.page = current
      this.handleRefresh()
    }

  }
}
</script>

<style lang="less" scoped>
// 分页器
.pagination {
  margin-top: 16px;

  .ant-pagination {
    float: right;
  }
}

.ant-table {
  table {
    // table-layout: fixed;
    border: none;
    border-collapse: collapse;
  }

  .ant-table-bnt {
    margin-bottom: 10px;
    display: -webkit-inline-box;
    display: -ms-inline-flexbox;
    display: inline-flex;
    justify-content: flex-end;
    width: 100%;

    button {
      margin-left: 10px;
    }

  }

  .ant-table-thead>tr {
    border: 1px solid #e8e8e8;
  }

  tr.order-empty {
    height: 15px;
    border: 1px solid #fff;

    td {
      padding: 0;
      border-right: none;
      border-left: none;
      background: none !important;
    }
  }
}

.ant-table-thead>tr>th {
  border-right: none;
  border-bottom: none;
  padding: 12px 12px;
  font-weight: bold;
}

.ant-table-tbody>tr>td {
  border-right: 1px solid #e8e8e8;
  border-left: 1px solid #e8e8e8;
  padding: 12px 12px;
  // text-align: center;
}

.tabCheBox {
  margin-right: 10px;
}

.tabOrdeno {
  display: flex;
  justify-content: space-between;

  .tabOrdeno_l {
    display: flex;
    align-items: center;
  }

  .tabOrdeno_r {
    font-weight: bold;
  }
}

.actions {
  display: flex;
  justify-content: space-around;
}
</style>
